﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DTcms.Common
{
    public static class Earth
    {
        /// <summary>  
        /// 地球的半径（公里）
        /// </summary>  
        public const double EARTH_RADIUS = 6378.137;

        /// <summary>  
        /// 计算经纬度坐标点的距离  
        /// </summary>  
        /// <param name="begin">开始的经度纬度</param>  
        /// <param name="end">结束的经度纬度</param>  
        /// <returns>距离(公里)</returns>  
        public static double GetDistance(LongLatPoint begin, LongLatPoint end)
        {
            double lat = begin.RadLat - end.RadLat;
            double lng = begin.RadLng - end.RadLng;

            double dis = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(lat / 2), 2) + Math.Cos(begin.RadLat) * Math.Cos(end.RadLat) * Math.Pow(Math.Sin(lng / 2), 2)));
            dis = dis * EARTH_RADIUS;
            dis = Math.Round(dis * 1e4) / 1e4;

            return dis;
        }

        /// <summary>
        /// 百度地理位置转化URl
        /// </summary>
        private static string CoordsUrl = "http://api.map.baidu.com/geoconv/v1/";
        /// <summary>
        /// 根据输入的经纬度List转化为百度地图的经纬度List，一一对应
        /// </summary>
        /// <param name="citem">要转化的经纬度List</param>
        /// <returns>转化后的经纬度List</returns>
        public static List<LongLatPoint> GetCoords(List<LongLatPoint> citem)
        {
            HttpHelper http = new HttpHelper();
            string strCoords = ListToString(citem);
            HttpItem item = new HttpItem()
            {
                URL = string.Format("{0}?coords={1}&from=3&to=5&ak=17bc43866bbd51f7507e0c618f890e64", CoordsUrl, strCoords),//URL     必需项    
            };
            HttpResult result = http.GetHtml(item);
            string html = result.Html;
            citem = StringToList(html);
            return citem;
        }

        /// <summary>
        /// 根据输入的经纬度转化为百度地图的经纬度
        /// </summary>
        /// <param name="citem">要转化的经纬度</param>
        /// <returns>转化后的经纬度List</returns>
        public static LongLatPoint GetCoords(LongLatPoint citem)
        {
            List<LongLatPoint> list = new List<LongLatPoint>();
            list.Add(citem);
            list = GetCoords(list);
            if (list != null & list.Count > 0)
            {
                return list[0];
            }
            return null;
        }

        /// <summary>
        /// 根据List转化为相应的字符串经纬度
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private static string ListToString(List<LongLatPoint> item)
        {
            StringBuilder strCoords = new StringBuilder();
            foreach (var obj in item)
            {
                if (string.IsNullOrEmpty(strCoords.ToString()))
                {
                    strCoords.Append(obj.Lat + "," + obj.Lng);
                }
                else
                {
                    strCoords.Append(";" + obj.Lat + "," + obj.Lng);
                }
            }
            return strCoords.ToString();
        }

        //{"status":0,"result":[{"x":114.22539195429,"y":29.581585367458},{"x":114.2253919533,"y":29.581585366942}]}
        //{"status":24,"message":"param error:coords format error","result":[]}
        /// <summary>
        /// 根据Json格式转化为List
        /// </summary>
        /// <param name="str">Json字符串</param>
        /// <returns></returns>
        private static List<LongLatPoint> StringToList(string str)
        {
            List<LongLatPoint> item = new List<LongLatPoint>();
            //json to list
            object obj = JsonHelper.jsonDes<CoordsList>(str);

            CoordsList c = (CoordsList)obj;
            return c.result;
        }
    }

    /// <summary>  
    /// 代表经度, 纬度  
    /// </summary>  
    public class LongLatPoint
    {
        /// <param name="lat">纬度 X</param>  
        /// <param name="lng">经度 Y</param>  
        public LongLatPoint(double lat, double lng)
        {
            this.lat = lat;
            this.lng = lng;
        }

        //  纬度 X  
        private double lat;

        // 经度 Y  
        private double lng;

        /// <summary>  
        /// 代表纬度 X轴  
        /// </summary>  
        public double Lat 
        {
            get { return this.lat; }
        }

        /// <summary>  
        /// 代表经度 Y轴  
        /// </summary>  
        public double Lng
        {
            get { return this.lng; }
        }

        public double RadLat { get { return lat * Math.PI / 180; } }

        public double RadLng { get { return lng * Math.PI / 180; } }
    }

    /// <summary>
    /// 返回参数类
    /// </summary>
    public class CoordsList
    {
        /// <summary>
        /// 状态0为成功其他不成功
        /// </summary>
        public int status { get; set; }
        /// <summary>
        /// result不为0时才显示
        /// </summary>
        public string message { get; set; }
        /// <summary>
        /// 经纬度列表
        /// </summary>
        public List<LongLatPoint> result { get; set; }
    }
}
